package com.dyl.common.core.domain;

import com.dyl.common.core.constant.Constants;

import java.io.Serializable;

/**功能描述：响应信息主体
 * @Auther:邓宇凌
 * @Date:2023/9/24
 * @Description:com.dyl.common.core.domain
 * @version:1.0
 */
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = Constants.SUCCESS;

    /** 失败 */
    public static final int FAIL = Constants.FAIL;

    private int code;

    private String message;

    private T data;

    private int count;

    public static <T> R<Object> ok()
    {
        return restResult(null, SUCCESS, null,0);
    }

    public static <T> R<T> ok(T data)
    {
        return restResult(data, SUCCESS, null,0);
    }

    public static <T> R<T> ok(T data, String msg)
    {
        return restResult(data, SUCCESS, msg,0);
    }

    public static <T> R<T> fail()
    {
        return restResult(null, FAIL, null,0);
    }

    public static <T> R<T> fail(String msg)
    {
        return restResult(null, FAIL, msg,0);
    }

    public static <T> R<T> fail(T data)
    {
        return restResult(data, FAIL, null,0);
    }

    public static <T> R<T> fail(T data, String msg)
    {
        return restResult(data, FAIL, msg,0);
    }

    public static <T> R<T> fail(int code, String msg)
    {
        return restResult(null, code, msg,0);
    }

    private static <T> R<T> restResult(T data, int code, String msg,int count)
    {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        apiResult.setCount(count);
        return apiResult;
    }

    public int getCode()
    {
        return code;
    }
    public int getCount()
    {
        return count;
    }

    public void setCode(int code)
    {
        this.code = code;
    }
    public void setCount(int count)
    {
        this.count = count;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }

}
